define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('AndonDetailService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$log',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $log) {
            this.init = function (options) {
                console.log("==>AndonDetailService==>init:" + JSON.stringify(options));
                var nowdatetime = $filter('date')(new Date(), 'yyyy/MM/dd HH:mm:ss');
                var dialogOpen = function (detailData) {
                    console.log("==>dialogOpen==>detailData:" + JSON.stringify(detailData));
                    $mdDialog.dialog(
                        'module/moduleSLI/program/SLI01/AndonDetail.html',
                        function (dialog) {
                            return {
                                css_url: 'module/moduleSLI/program/SLI01/AndonDetail.css',
                                andonDetailInfo: detailData.slightDetailInfo[0],
                                beforeShown: function () {
                                    // 20220727 modify by Alan for#100246 : 修正顯示名稱
                                    // 設定緊急度，安燈現況，響應等級
                                    dialog.andonDetailInfo.GradeName = dialog.getName('Grade');
                                    dialog.andonDetailInfo.SLightStateName = dialog.getName('SLightState');
                                    dialog.andonDetailInfo.ResponseLevelName = dialog.getName('ResponseLevel');
                                    // 取得原因列表
                                    andon_reason_no_get(dialog.andonDetailInfo.SLightTypeNo, function(reasonList) {
                                        // 設定原因名稱
                                        const slightReason = $filter('filter')(reasonList, { slightreasonno: dialog.andonDetailInfo.SLightReasonNo });
                                        if(slightReason.length > 0) {
                                            dialog.andonDetailInfo.SLightReasonName = slightReason[0].reasonname;
                                        } else {
                                            dialog.andonDetailInfo.SLightReasonName = '';
                                        }
                                    });
                                },
                                back: function () {
                                    dialog.hide();
                                },
                                //20170516 modify by Dustdusk for ��������ԭ��δݔ��Ŀ���
                                confirm: function () {
                                },
                                getName: function(prop) {
                                    // 20220727 modify by Alan for#100246 : 修正顯示名稱
                                    return $filter('translate')(`AndonDetail.${ prop }.${ dialog.andonDetailInfo[prop] }`);
                                }
                            };
                        }
                    );
                };

                var getAndonDetailInfo = function () {
                    $log.log("==>getAndonDetailInfo==>id:" + options.id);
                    let content = {
                        ID: options.id
                    }
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_SLI.slight_detail_info_get',
                        content: content,
                        success: function (data) {
                            $log.log("==>data:" + JSON.stringify(data));
                            dialogOpen(data);
                        }
                    });
                };
                
                // 20220727 modify by Alan for#100246 : 修正顯示名稱
                var andon_reason_no_get = function(sLightTypeNo, callback) {
                    $MMWService.pageNumberInfoGet({
                        page_mode : 'andon_reason_no_get',
                        query_condition_info:[
                            {column_name: 'SLightTypeNo', query_mode: '0', value: sLightTypeNo, merge_condition_model: 'AND'}
                        ]
                    }, function(itemList){
                        callback && callback(itemList);
                    });
                };
                
                (function(){
                    getAndonDetailInfo();
                })();
            };
        }]);
});
